local voucherId = ARGV[1];
local userID = ARGV[2];

local voucherStockKey = KEYS[1] .. voucherId;
local voucherOrderKey = KEYS[2] .. voucherId;

-- 判断库存    读取库存voucher key 小于1 返回 1
if tonumber(redis.call("get", voucherStockKey)) < 1 then
    return 1;
end
--判断重复下单   is member set userid
if redis.call("sismember", voucherOrderKey, userID) == 1 then
    return 2;
end

--扣减库存   incr by  -1
redis.call("incrby", voucherStockKey, -1);
--保存下单记录   set add userid -> set
redis.call("sadd",voucherOrderKey,userID);
return 0;